angular.module('starter')
.controller('selectAreaCtrl', function ($scope, $http, $state,httpService, appBase) {
	
	var selectL = document.querySelector(".areaCon .left ul");
    var selectR = document.querySelector(".areaCon .right ul");

    var showProvince = document.querySelector(".province");
    var showCity = document.querySelector(".city");
    var showCounty = document.querySelector(".county"); 
    var provinceCon = document.querySelector(".province i");
    var cityCon = document.querySelector(".city i");
	
    loadArea();
	
	console.log(appBase.cityCode);
	$scope.affirmArea = function(){//确定地区
		//返回
		
	    appBase.cityCode = cityCon.value;
	    appBase.cityName = cityCon.innerText;

	    $state.go('home', {}, { reload: true });
	    //$state.go("home", {}, { reload: true });
	 
	}


	function loadArea() {
	    var parData = "?type=All&code=" + appBase.cityCode;
 
	    httpService.get(httpService.appWebApi.getAreaCode + parData
          ).then(
          function (success) {
              var data = success.data;
              if (data.returnCode != "0") {
                  console.log(data.returnMsg)
                  return;
              }
 
                  var bol = false;
                  var provinceIndex = 0;
                  var cityIndex = 0;
                  var provinceList = data.returnData;

                  // ------------------------------------------------------

                  function createOption(obj, arr, value, fn) {
                      obj.innerHTML = "";

                      var df = document.createDocumentFragment();
                      for (var i = 0; i < arr.length; i++) {
                          var li = document.createElement("li");
                          li.innerHTML = arr[i][value];
                          li.value = arr[i]["code"];
                          df.appendChild(li);
                      }
                      obj.appendChild(df);

                      fn && fn(obj);
                  }
                  // ------------------------------------------------------

                  function ff() {
                      createOption(selectL, provinceList, "name", function (obj) {
                          // 添加点击事件
                          var lastIndex = 0;
                          for (var i = 0; i < obj.children.length; i++) {
                              obj.children[i].index = i;
                              obj.children[i].onclick = function () {

                                  showProvince.classList.remove("actived");
                                  showProvince.classList.add("activing");
                                  showCity.classList.remove("actived");
                                  showCity.classList.remove("activing");
                                  cityCon.innerText = "选城市";

                                  // 更改背景色
                                  obj.children[1].style.backgroundColor = "";
                                  obj.children[lastIndex].style.background = "";
                                  this.style.backgroundColor = "white";
                                  lastIndex = this.index;
                                  //显示所选省
                                  provinceCon.innerText = provinceList[lastIndex]['name'];
                                  // 更改selectR的内容
                                  provinceIndex = this.index;
                                  var cityList = provinceList[provinceIndex].city;

                                  $(".bar button.affirmArea").attr("disabled", "disabled");//确定按钮不可点击

                                  // ------------------------------------------------------

                                  createOption(selectR, cityList, "name", function (obj) {
                                      var lastIndex = 0;
                                      for (var i = 0; i < obj.children.length; i++) {
                                          obj.children[i].index = i;
                                          obj.children[i].onclick = function () {

                                              lastIndex = this.index;
                                              //显示所选市
                                              showCity.classList.remove("actived");
                                              showCity.classList.add("activing");
                                              showProvince.classList.remove("activing");
                                              showProvince.classList.add("actived");

                                              cityCon.innerText = provinceList[provinceIndex].city[lastIndex]['name'];
                                              cityCon.value = provinceList[provinceIndex].city[lastIndex]['code'];

                                              $(".bar button.affirmArea").attr("disabled", false);//确定按钮可以点击
                                          }
                                      }
                                  });
                              }
                          }
                      });
                  }
                  ff();
                  // 初始化默认为“广东”
                  selectedFn(appBase.cityCode);
                  // ------------------------------------------------------
                  // 默认被选中
                  function selectedFn(value) {
                      for (var i in selectL.children) {
                          if (value.substr(0,2) == selectL.children[i].value) {
                              selectL.children[i].onclick();
                              for (var ir in selectR.children) {
                                  if (value.substr(0, 4) == selectR.children[ir].value ||
                                      (value.substr(0, 4) == "0000" && selectR.children[ir].value == 0)) {
                                      selectR.children[ir].onclick();
                                  }
                              }
                              break;
                          };
                      }
                  }
          

          }
          , function (error) {
              console.log(error);
             
          });
	}
})
